package mysql

import (
	"day15/app_config"
	"fmt"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"sync"
)

var (
	Db   *gorm.DB
	err  error
	once sync.Once
)

func InitMysql() {
	appConf := app_config.AppConf.MysqlConfig
	dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8&parseTime=True", appConf.User, appConf.Password, appConf.Host, appConf.Port, appConf.Database)
	once.Do(func() {
		Db, err = gorm.Open(mysql.Open(dsn))
		if err != nil {
			fmt.Println("数据库连接失败")
		}
	})
	fmt.Println("数据库连接")
	Db.AutoMigrate(new(User), new(House))
}
